//路由鉴权：路由能不能被访问到权限的设置->全局守卫完成
//引入路由器
import router from "@/router";
//引入用户相关的仓库
import useUserStore from '@/store/modules/user.ts'
//引入大仓库
import pinia from "@/store";

let userStore = useUserStore(pinia)

//引入进度条
import Nprogress from 'nprogress'
//引入进度条的样式
import 'nprogress/nprogress.css'
//进度条的个性化
Nprogress.configure({showSpinner: false})

//存储路由白名单
let whiteList = [
    '/home',

    '/hospital/register',

    '/hospital/detail',

    '/hospital/notice',

    '/hospital/close',

    '/hospital/search',
]

//添加相应的全局守卫
//前置守卫
// @ts-ignore
router.beforeEach((to, from, next) => {
    Nprogress.start()
    //动态设置网页标题
    document.title = `尚医通-${to.meta.title}`
    //判断用户是否登录：根据token
    let token = userStore.userInfo.token
    if (token) {
        //已登
        next()
    } else {
        //未登
        if(whiteList.includes(to.path)){
            next()
        }else {
            //登陆组件显示出来
            userStore.visible=true
            next({path:'/home',query:{
                redirect:to.fullPath
                }})
        }
    }


})
//后置守卫
// @ts-ignore
router.afterEach((to, from) => {
    Nprogress.done()
})